package cn.wegoteam.shop.service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.wegoteam.shop.inter.BaseDaoInter;
import cn.wegoteam.shop.inter.OrderServiceInter;
import cn.wegoteam.shop.inter.PayImpServiceInter;
import cn.wegoteam.shop.inter.PayServiceInter;
import cn.wegoteam.shop.po.Order;
import cn.wegoteam.shop.po.User;
import cn.wegoteam.shop.util.Const;
import cn.wegoteam.shop.util.DataUtils;
@Service
public class PayService extends BaseService<Order> implements PayServiceInter {
	private PayImpServiceInter payImpService;
	@Autowired
	private OrderServiceInter orderService; 
	@Resource(name="orderDao")
	public void setBaseDao(BaseDaoInter<Order> baseDao) {
		super.setBaseDao(baseDao);
	}
	/**
	 * 支付
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void pay(Order order,User user,HttpServletRequest request){
		if(order.getPayMethod().equals(Const.STATIC_ALIPAY)){
			payImpService = new AlipayService();
		}
		payImpService.pay(order, user, request);
	}
	/**
	 * 支付成功处理
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean paySuccess(HttpServletRequest request, String out_trade_no,
			String trade_no, String buyer_email){
		Order o = orderService.getList(null, DataUtils.getMap("orderNumber",out_trade_no)).get(0);
		o.setTradeno(trade_no);
		o.setState(Const.STATIC_PAID);
		orderService.update(o);
		return true;
	}
	
}
